class Solution {
public:
    int GetRef(vector<int>& stock, int begin, int end)
    {
        return stock[rand() % (end - begin + 1) + begin];
    }
    void qsort(vector<int>& stock, int begin, int end, int k)
    {
        if (begin >= end) return;
        int ref = GetRef(stock, begin, end);
        int left = begin - 1, right = end + 1;
        for (int i = begin; i < right; )
        {
            if (stock[i] < ref) swap(stock[i++], stock[++left]);
            else if (stock[i] == ref) i++;
            else swap(stock[i], stock[--right]);
        }
        int a = left - begin + 1, b = right - left - 1;
        if (a > k) qsort(stock, begin, left, k);
        else if (a + b >= k) return;
        else qsort(stock, right, end, k - a - b);
    }
    vector<int> inventoryManagement(vector<int>& stock, int cnt)
    {
        srand(time(nullptr));
        qsort(stock, 0, stock.size() - 1, cnt);
        return { stock.begin(), stock.begin() + cnt };
    }
};
class Solution {
public:
    int GetRef(vector<int>& nums, int begin, int end)
    {
        return nums[rand() % (end - begin + 1) + begin];
    }
    int _findKthLargest(vector<int>& nums, int begin, int end, int k)
    {
        if (begin == end) return nums[begin];
        int ref = GetRef(nums, begin, end);
        int left = begin - 1, right = end + 1;
        for (int i = begin; i < right; )
        {
            if (nums[i] < ref) swap(nums[i++], nums[++left]);
            else if (nums[i] == ref) i++;
            else swap(nums[i], nums[--right]);
        }
        int a = end - right + 1, b = right - left - 1;
        if (a >= k) return _findKthLargest(nums, right, end, k);
        else if (a + b >= k) return ref;
        else return _findKthLargest(nums, begin, left, k - a - b);
        return 0;
    }
    int findKthLargest(vector<int>& nums, int k)
    {
        srand(time(nullptr));
        return _findKthLargest(nums, 0, nums.size() - 1, k);
    }
};